﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GDISim._Infrastructure._Hardware._Datacenter;
using System.Xml.Serialization;
using GDISim._Infrastructure._Software._Operation;

namespace GDISim._Infrastructure._Software._Job
{
    public class JobFrequency:ILoad
    {
        #region Members
        private DatacenterName dcName;
        private JobName jobName;
        private List<OperationFrequency> operationFrequencyList;
        #endregion

        #region Constructor
        public JobFrequency()
        {
        }
        #endregion

        #region Properties
        [XmlAttribute("DatacenterName")]
        public DatacenterName DcName
        {
            get
            {
                return this.dcName;
            }
            set
            {
                this.dcName = value;
            }
        }
        [XmlAttribute("JobName")]
        public JobName JobName
        {
            get
            {
                return this.jobName;
            }
            set
            {
                this.jobName = value;
            }
        }
        [XmlArray("OperationFrequencyList"), XmlArrayItem(typeof(OperationFrequency))]
        public List<OperationFrequency> OperationFrequencyList
        {
            get
            {
                return this.operationFrequencyList;
            }
            set
            {
                this.operationFrequencyList = value;
            }
        }
        #endregion

        #region Methods
        public List<OperationRequest> GenerateOperationRequestList()
        {
          
            Dictionary<OperationFrequency, double> operationFrequencyDictionary = new Dictionary<OperationFrequency, double>();
            List<OperationRequest> operationRequestList = new List<OperationRequest>();

            foreach (OperationFrequency od in this.operationFrequencyList)
            {
                operationFrequencyDictionary.Add(od, od.Frequency);
            }
     
            foreach (OperationFrequency ot in operationFrequencyDictionary.Keys)
            {
                if (ot.Type.Equals(OperationFrequency.FrequencyType.INTERVAL))
                {
                    TimeSpan startTime = new TimeSpan(0);
                    for (int i = 0; i < 24; i++)
                    {
                        for (int j = 0; j < 60; j++)
                        {
                            if (startTime.Minutes % operationFrequencyDictionary[ot] == 0)
                            {
                                OperationRequest or = new OperationRequest();
                                or.OperationId = System.Guid.NewGuid();
                                or.OperationType = ot.Operation;
                                or.OperationTime = startTime.Add(new TimeSpan(0, 0, 0));
                                operationRequestList.Add(or);
                            }
                            startTime = startTime.Add(new TimeSpan(0, 1, 0));
                        }
                    }

                }
                else if (ot.Type.Equals(OperationFrequency.FrequencyType.GAP))
                {
                    OperationRequest indexOp = new OperationRequest();
                    indexOp.OperationId = System.Guid.NewGuid();
                    indexOp.OperationType = OperationType.IDX_INDEXBUILD;
                    indexOp.OperationTime = new TimeSpan(0, 0, 0);
                    operationRequestList.Add(indexOp);
                }
            }

            operationRequestList.Sort(OperationRequest.CompareOperationRequestTimes);
            return operationRequestList;

        }

        #endregion
    }
}
